home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2070 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: isis.interpac.net!stacys
  2. From: stacys@isis.interpac.net (Stacy Sherman)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Newbie question:  Is this code OK?
  5. Date: 18 Jan 1996 21:33:40 GMT
  6. Organization: Inter-Pacific Networks
  7. Message-ID: <4dmebk$foq@pegasus.interpac.net>
  8. References: <4di986$fk1@pegasus.interpac.net> <30FDDAC8.D79@e-tex.com>
  9. NNTP-Posting-Host: isis.interpac.net
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Thanks everyone for all your comments.  It seems that the code was 
  13. generally OK, except for 1 error which one of you caught and made itself 
  14. known to me after I changed the program to check for '\0' instead of 
  15. i<length.  The second for loop should have an && between the i<length and 
  16. the ((string[i]... parts.  I'm still not real clear on this, I guess the 
  17. comma between two conditions acts as an 'or' instead of an 'and'?
  18.  
  19. Jos, in the netherlands came up with a simpler way to do this:
  20.  
  21. for (; *string; string++)
  22.  
  23.                         if (isspace(*string))
  24.                                 state= 1;
  25.                         else if (state) {
  26.                                 num_words++;
  27.                                 state= 0;
  28.                         }
  29.  
  30.                 return num_words;
  31. State being = 1 means it's reading white space.  If you wanted, you could 
  32. still replace the isspace with explicitly checking for the three chars 
  33. like I did, and change to an array instead of a pointer.
  34.  
  35. Other points:
  36.  
  37. I know that strings are normally *char and that arrays are normally 
  38. terminated by '\0' but the program called for an array where the length 
  39. was explicitly stated.  Everyone mentioned that there's an isspace or 
  40. iswhite function but I figured it would be cheating to use it.
  41.  
  42. Thanks alot.  I'm sure you haven't heard the last of me :)
  43.  
  44. Stacy
  45.  
  46. smelly@e-tex.com wrote:
  47. : > Do I need the empty {} after a for loop that has no body?  I assumed > 
  48. : > if I didn't, the loop would execute the next statement after it.
  49.  
  50.  
  51. : >                         for (; i<length, ((string[i]!='\n') &&
  52. : >                                 (string[i]!='\t') &&
  53. : >                                 (string[i]!=' ')); i++)
  54. : >                                  /* and skip past any other */
  55. : >                                  /* chars that may be there */
  56. : >                         { }     /* loop does all work, nothing inside body */
  57.  
  58. : I am also new to C programming; however, I think I can make one 
  59. : suggestion.  Since your second for loop does all the work and you don't 
  60. : need a body, you can write it like this.
  61.  
  62. :                          for ( ; i<length, ((string[i]!='\n') &&
  63. :                                  (string[i]!='\t') &&
  64. :                                  (string[i]!=' ')); i++);
  65.  
  66. : Placing the semi-colon at the end of the loop will prevent it from 
  67. : running the next statement following the loop until after it has 
  68. : processed to a point where the condition if false.  I believe this will 
  69. : work.  Good luck and have fun! :-)
  70.  
  71. : smelly@e-tex.com
  72. : Smelly Dog
  73.